<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-data">&nbsp;&nbsp;财务管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-notebook-1">&nbsp;&nbsp;固定资产</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">信息查询</el-divider>
      <el-form ref="form" :model="fixedAssetsForm" style="margin-left: 20px">
        <el-form-item class="fixAsset_formSubmit_search">
          <el-select v-model="fixedAssetsForm.assetName" placeholder="请选择资产名称"
                     @change="loadAssetNList()">
            <el-option
                v-for="item in fixAssetNameOptions"
                :key="item.id"
                :label="item.assetName"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-table :data="fixedAssets.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="assetId" label="资产编号" width="200px"></el-table-column>
        <el-table-column prop="assetName" label="资产名称" width="200px"></el-table-column>
        <el-table-column prop="assetClassification" label="资产分类" width="200px"></el-table-column>
        <el-table-column prop="originValueAsset" label="资产原值" width="200px"></el-table-column>
        <el-table-column prop="dateAcquisition" label="购置日期" width="200px"></el-table-column>
        <el-table-column prop="specificationModel" label="规格型号" width="200px"></el-table-column>
        <el-table-column prop="responsePerson" label="责任者" width="200px"></el-table-column>
        <el-table-column prop="remarks" label="备注" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                @click="openEditDialog(scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="fixedAssets.length">
        </el-pagination>
      </div>

      <!-- 以下是弹出的修改固定资产的对话框 -->
      <el-dialog
          width="80%"
          :modal-append-to-body="false"
          title="编辑固定资产"
          :visible.sync="dialogFormVisible">
        <el-form :model="fixedAssetsForm">
          <el-form-item label="编号" :label-width="formLabelWidth">
            <el-input v-model="fixedAssetsForm.assetId" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="资产名称" :label-width="formLabelWidth">
            <el-input v-model="fixedAssetsForm.assetName" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="资产分类" :label-width="formLabelWidth">
            <el-select v-model="fixedAssetsForm.assetClassification" placeholder="请选择分类">
              <el-option
                  v-for="item in assetClassifications"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="资产原值" :label-width="formLabelWidth">
            <el-input v-model="fixedAssetsForm.originValueAsset" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="购置日期" :label-width="formLabelWidth">
            <el-date-picker
                v-model="fixedAssetsForm.dateAcquisition"
                type="date"
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                placeholder="选择购置日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="规格型号" :label-width="formLabelWidth">
            <el-input v-model="fixedAssetsForm.specificationModel" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="责任者" :label-width="formLabelWidth">
            <el-input v-model="fixedAssetsForm.responsePerson" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="备注" :label-width="formLabelWidth">
            <el-input v-model="fixedAssetsForm.remarks" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false" type="warning" style="color:white;">取 消</el-button>
          <el-button type="primary" @click="submitEditForm()" style="color:white;">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fixAssetNameOptions: [],
      fixAssetClassOptions: [],
      assetClassifications: [
        {value: "数码设备", label: "数码设备"},
        {value: "机器设备", label: "机器设备"},
        {value: "办公设备", label: "办公设备"},
        {value: "房屋建筑", label: "房屋建筑"},
        {value: "公司财产", label: "公司财产"},
      ],
      dialogFormVisible: false,
      formLabelWidth: '120px',

      currentPage: 1,
      pageSize: 10,
      fixedAssets: [],
      fixedAssetsForm: {
        id: '',
        assetName: '',
        assetId: '',
        assetClassification: '',
        originValueAsset: '',
        dateAcquisition: '',
        specificationModel: '',
        responsePerson: '',
        remarks: ''
      },
    }
  },
  methods: {
    openEditDialog(financeFixedAsset) {
      let url = 'http://localhost:9080/financeFixedAssets/' + financeFixedAsset.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.fixedAssetsForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAssetList();
            }
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://localhost:9080/financeFixedAssets/' + this.fixedAssetsForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.fixedAssetsForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改固定资产成功！',
            type: 'success'
          });
          this.loadAssetList();
          this.dialogFormVisible = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadAssetList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openDeleteConfirm(financeFixedAsset) {
      let message = '此操作将永久删除【' + financeFixedAsset.assetName + '】固定资产，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(financeFixedAsset);
      }).catch(() => {
      });
    },
    handleDelete(financeFixedAsset) {
      let url = 'http://localhost:9080/financeFixedAssets/' + financeFixedAsset.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除固定资产成功！',
            type: 'success'
          });
          local.reload();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadAssetList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadAssetList() {
      let url = 'http://localhost:9080/financeFixedAssets';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        console.log('message=' + responseBody.message);
        this.fixedAssets = responseBody.data;
        console.log('fixedAssets=' + responseBody.data);
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadAssetList();
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.fixAsset_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>